package com.novellotus.iov.carvice.model;

import android.widget.Button;
import android.widget.EditText;

import com.jakewharton.rxbinding2.widget.RxTextView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.activity.ModifyPasswordActivity;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.rxjava.MyObserver;
import com.novellotus.iov.carvice.utils.DataSign;
import com.novellotus.iov.carvice.utils.MyUtils;

import io.reactivex.Observable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;

/**
 * Created by Rich on 2017/8/23.
 */

public class ModifyPasswordModel {

    private ModifyPasswordActivity context;

    private Observable<CharSequence> observableOld;
    private Observable<CharSequence> observableNew;

    public ModifyPasswordModel(ModifyPasswordActivity modifyPasswordActivity) {
        this.context = modifyPasswordActivity;
    }

    /**
     * 设置旧密码编辑框
     */
    public void setEditOld(final EditText editOld) {

        observableOld = RxTextView.textChanges(editOld);
        observableOld.compose(RxSchedulers.<CharSequence>lifecycle(context)).subscribe(new MyObserver<CharSequence>() {
            @Override
            public void onNext(CharSequence value) {
                super.onNext(value);
                if (value.length() >= 6) {
                    editOld.setTextColor(context.getResources().getColor(R.color.text_black));
                } else {
                    editOld.setTextColor(context.getResources().getColor(R.color.textTipsRed));
                }
            }
        });
    }

    /**
     * 设置新密码编辑框
     */
    public void setEditNew(final EditText editNew) {
        observableNew = RxTextView.textChanges(editNew);
        observableNew.compose(RxSchedulers.<CharSequence>lifecycle(context)).subscribe(new MyObserver<CharSequence>() {
            @Override
            public void onNext(CharSequence value) {
                super.onNext(value);
                if (value.length() >= 6) {
                    editNew.setTextColor(context.getResources().getColor(R.color.text_black));
                } else {
                    editNew.setTextColor(context.getResources().getColor(R.color.textTipsRed));
                }
            }
        });
    }

    /**
     * 表单验证
     */
    public void setConfirm(final Button btn_right) {
        Observable.combineLatest(observableOld, observableNew, new BiFunction<CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean apply(CharSequence oldPsw, CharSequence newPsw) throws Exception {
                return oldPsw.length() >= 6 && newPsw.length() >= 6 && !oldPsw.toString().equals(newPsw.toString());
            }
        }).compose(RxSchedulers.<Boolean>lifecycle(context)).subscribe(new Consumer<Boolean>() {

            @Override
            public void accept(Boolean pass) throws Exception {
                if (pass) {
                    btn_right.setTextColor(context.getResources().getColor(R.color.white));
                    btn_right.setClickable(true);
                } else {
                    btn_right.setTextColor(context.getResources().getColor(R.color.gray_line));
                    btn_right.setClickable(false);
                }
            }
        });
    }

    /**
     * 修改密码
     */
    public void changePassword(String sign) {
        Observable<BaseEntity<Boolean>> observable = RetrofitFactory.getInstance().changePassword(DataSign.getSign(sign, context));
        observable.compose(RxSchedulers.<BaseEntity<Boolean>>lifecycle(context)).compose(RxSchedulers.<BaseEntity<Boolean>>compose())
                .subscribe(new BaseObserver<Boolean>(context) {
                    @Override
                    public void onSuccess(Boolean response) {
                        MyUtils.showToast(context, "修改密码成功!");
                        context.finish();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        MyUtils.showToast(context, msg);
                    }

                    @Override
                    public void onError(String msg) {
                        MyUtils.showToast(context, msg);
                    }
                });
    }
}
